# 字典
# 定义字典，键是唯一的
person = {
    'name': 'name1', 
    'gender': 'nan', 
    'age': 21
}
print(person, type(person))

book = {
    'title': 'python',
    'price': 49.9,
    'author': 'xiaoding',
    'pub': 'huadong',
    'pubtime': '2024-10-1'
}
print(book)
phone = {
    'brand': 'phonename',
    'color': 'bule',
    'price': 5000,
    'size': '12*12',
    'memory': 4069,
    'cpu': 2
}
print(phone)

# 通过键访问
print(phone['price'])
# 存在返回值，如果不存在返回None
print(phone.get('disk'))
# 设置返回默认值
print(phone.get('disk', '256GB'))
# 成员判断
print('brand' in phone)

# 获取所有的键 
print(person.keys())            # dict_keys(['name', 'gender', 'age'])
# 获取所有的值
print(person.values())          # dict_values(['name1', 'nan', 21])
# 获取所有的键值对
print(person.items())           # dict_items([('name', 'name1'), ('gender', 'nan'), ('age', 21)])

# 统计长度
print(len(book))
#遍历所有的键
for k in person.keys():
    print(k, person[k])
# 遍历所有的值
for v in person.values():
    print(v
# 遍历所有的键值对
for item in person.items():
    print(item)

# 修改添加，单独修改
print(book)
book['price'] = 78
print(book)
book['is_onsale'] = True
print(book)
# 批量操作
book.update({'price': 67, 'is_onsale': False})
print(book)

# 删除 
del book['author']
print(book)
book.pop('pub')
print(book)
# 清空整个字典
book.clear()
print(book)



